(function($, window, document) {

	$.fn.uploader = function(options) {
		var self = this;
		var o = $.extend({}, {
			maxSize : "10m",
			fileTypes : undefined,
			uploadUrl : undefined,
			downloadUrl : undefined,
			plupload : undefined,
			sourceId : undefined,
			sourceType:undefined,
			readonly : false
		}, options);
		o.id = self[0].id;
		var $result = {
			uploader : self,
			openUploaderDialog : function() {
				$.each(self.pluploadQueue().files, function(i, file) {
					self.pluploadQueue().removeFile(file);
				});
				$("#" + o.id + "Dialog").dialog("open");
			},
			deleteFile : function(id) {
				isperp.confirmAlert('确定删除此文件？<label style="color:red">（数据被删除后无法恢复！）', {
					yes : function() {
						self.find("#" + id).remove();
						// 远程删除附件在文件
						$.post("app/uploadFile/deleteFile/"+id,{id:id}, function(data) {
							$("#File-" + id).remove();
						});
					}
				});
			},
			showFile : function(id) {
				$("#" + o.id + "FileInfoName,#" + o.id + "FileInfoPath,#" + o.id + "FileInfoSize,#" + o.id + "FileInfoModifier,#" + o.id + "FileInfoModifyDate").empty();
				$("#" + o.id + "FileInfoName").attr("href", "");
				$.post("app/uploadFile/getFileInfo/"+id,{id:id,sourceType:o.sourceType}, function(data) {
					// 通过返回的数据显示文件信息
					$("#" + o.id + "FileInfoName").text(data.name);
					$("#" + o.id + "FileInfoName").attr("href", o.downloadUrl + '?id=' + data.id+'&sourceType='+data.sourceType);
					$("#" + o.id + "FileInfoPath").html(data.path);
					$("#" + o.id + "FileInfoSize").html(Math.round(data.size / 1024));
					$("#" + o.id + "FileInfoModifier").html(data.modifierName);
					$("#" + o.id + "FileInfoModifyDate").html(data.modifyDate);
					$("#" + o.id + "FileInfo").dialog("open");
				});
			},
			reload : function() {
				$.get("app/uploadFile/getBySourceId", {
					"sourceId" : o.sourceId,
					"sourceType":o.sourceType
				}, function(data) {
					$("#" + o.id + "Files").html("");
					$(data).each(function(index, item) {
						var imgStr="src='img/icons/"+item.ext+".gif'";
						if(areImgFile(item.ext))
					    {
							imgStr="width='100px' height='100px' ltImg='t' src='"+item.path+"'";
					    }
						var deleteStr="<a href=\"javascript:;\"  class='nui-txt-link' onclick='" + o.id + ".deleteFile(\"" + item.id + "\")'>删除</a>";
						if(o.readonly)
							deleteStr="";
						$("#" + o.id + "Files").append("<div class=\"att_bt attachitem\" id=\"File-" + item.id + "\"><div class=\"ico_big\">" +
								"<a></a><img  "+imgStr+"/>" +
								"</div><div class=\"name_big\" <span>"+ item.name +"</span><span class=\"graytext\">&nbsp;("+bytesToSize(item.size)+")</span><div class=\"down_big\">" +
								deleteStr +
								"<a href=\"fileDownload?id="+item.id+"\" class='nui-txt-link'>下载</a></div></div></div>");
					});
					$("[ltImg]").click(function(){
						  showImg($(this).attr("src"));
					});
				});
			}
		}, _init = function() {

			if (!o.readonly) {
				self.pluploadQueue($.extend({
					runtimes : "html5,flash,html4",
					url : o.uploadUrl + "?sourceId=" + o.sourceId+"&sourceType="+o.sourceType,
					max_file_size : o.maxSize,
					chunk_size : '100mb',
					unique_names : true,
					filters : [ {
						title : "选择文件",
						extensions : o.fileTypes
					} ],
					flash_swf_url : "js/libs/forms/uploader/plupload.flash.swf"
				}, self.data()));

				self.find('.plupload_button').addClass('button grey btn');
				self.find('.plupload_add').addClass('icon-plus');
				self.find('.plupload_start').addClass('icon-ok');

				self.plupload = self.pluploadQueue();

				self.pluploadQueue().bind("FileUploaded", function(up, file, responseData) {
					responseData = responseData.response;
					if (file.status == 5) {
						file.status = 10;
						var item = eval("(" + responseData + ")");
						item.modifyDate = new Date(item.modifyDate).format("yyyy-MM-dd");
						var imgStr="src='img/icons/"+item.ext+".gif'";
						if(areImgFile(item.ext))
					    {
							imgStr="width='100px' height='100px' ltImg='t' src='"+item.path+"'";
					    }
						var deleteStr="<a href=\"javascript:;\"  class='nui-txt-link' onclick='" + o.id + ".deleteFile(\"" + item.id + "\")'>删除</a>";
						if(o.readonly)
							deleteStr="";
						$("#" + o.id + "Files").append("<div class=\"att_bt attachitem\" id=\"File-" + item.id + "\"><div class=\"ico_big\">" +
								"<a></a><img  "+imgStr+"/>" +
								"</div><div class=\"name_big\" <span>"+ item.name +"</span><span class=\"graytext\">&nbsp;("+bytesToSize(item.size)+")</span><div class=\"down_big\">" +
								deleteStr +
								"<a href=\"fileDownload?id="+item.id+"\" class='nui-txt-link' >下载</a></div></div></div>");
					}
					$("[ltImg]").click(function(){
						showImg($(this).attr("src"));
					});
				});

				self.pluploadQueue().bind("UploadComplete", function(up, files) {
					// $("#selfDialog").dialog("close");
					self.find(".plupload_buttons").css("display", "inline");
					self.find(".plupload_upload_status").css("display", "inline");
				});

				$("#" + o.id + "Dialog").dialog({
					width : "560",
					modal : false,
					autoOpen : false,
					resizable : false,
					buttons : {
						"关闭" : function() {
							$(this).dialog("close");
						}
					}
				});
			}

			$.get("app/uploadFile/getBySourceId", {
				"sourceId" : o.sourceId,
				"sourceType":o.sourceType
			}, function(data) {
				$(data).each(function(index, item) {
					var imgStr="src='img/icons/"+item.ext+".gif'";
					if(areImgFile(item.ext))
				    {
						imgStr="width='100px' height='100px' ltImg='t' src='"+item.path+"'";
				    }
					var deleteStr="<a href=\"javascript:;\"  class='nui-txt-link' onclick='" + o.id + ".deleteFile(\"" + item.id + "\")'>删除</a>";
					if(o.readonly)
						deleteStr="";
					$("#" + o.id + "Files").append("<div class=\"att_bt attachitem\" id=\"File-" + item.id + "\"><div class=\"ico_big\">" +
							"<a></a><img  "+imgStr+"/>" +
							"</div><div class=\"name_big\" <span>"+ item.name +"</span><span class=\"graytext\">&nbsp;("+bytesToSize(item.size)+")</span><div class=\"down_big\">" +
							deleteStr +
							"<a href=\"fileDownload?id="+item.id+"\" class='nui-txt-link'>下载</a></div></div></div>");
				});
				$("[ltImg]").click(function(){
					showImg($(this).attr("src"));
				});
			});
			$("#" + o.id + "FileInfo").dialog({
				width : "435",
				height : "300",
				modal : false,
				autoOpen : false,
				resizable : false,
				buttons : {
					"关闭" : function() {
						$(this).dialog("close");
					}
				}
			});
		};
		_init();
		return $result;
	}

})(jQuery, this);
